package com.itheima.train.manager;

import com.alibaba.fastjson.JSON;
import com.itheima.train.manager.domain.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;

@SpringBootApplication
public class SynDataToRedisApplication {

    private static List<String> userNames = new ArrayList<String>() ;
    static {
        userNames.add("王志文") ;
        userNames.add("李丽丽") ;
        userNames.add("赵雪") ;
        userNames.add("马丽雯") ;
        userNames.add("刘丽莎") ;
        userNames.add("李文武") ;
        userNames.add("赵宇") ;
        userNames.add("刘成伟") ;
        userNames.add("张哲轩") ;
        userNames.add("李思远") ;
    }

    public static void main(String[] args) {
        ConfigurableApplicationContext applicationContext = SpringApplication.run(SynDataToRedisApplication.class, args);// 启动程序
        // initUserInfo(applicationContext) ;
        // deleteAllKeys(applicationContext);
    }

    // 对用户数据进行初始化的操作
    private static void initUserInfo(ConfigurableApplicationContext applicationContext) {

        StringRedisTemplate redisTemplate = applicationContext.getBean(StringRedisTemplate.class);
        // redisTemplate.delete("train_manager:userInfo") ;
        for (String userName : userNames) {
            User user = new User() ;
            String userId = UUID.randomUUID().toString().replace("-" , "") ;
            user.setId(userId);
            user.setUserName(userName);
            user.setPhone("13121031054");
            redisTemplate.boundHashOps("train_manager:userInfo").put(userId , JSON.toJSONString(user));
        }

        System.out.println("对登录用户初始化完毕.........");

    }

    // 删除Redis中的所有的数据
    public static void deleteAllKeys(ConfigurableApplicationContext applicationContext) {
        System.out.println("数据删除开始...............");
        StringRedisTemplate redisTemplate = applicationContext.getBean(StringRedisTemplate.class);
        // Set<String> keys = redisTemplate.keys("riding_plan:*");
        Set<String> keys = redisTemplate.keys("train_seat:*");
        System.out.println(keys.size());
        keys.forEach( key -> {
            System.out.println(key);
            redisTemplate.delete(key) ;
        });
        System.out.println("数据删除完毕...............");
    }

}
